package com.mission.schedule.my160920.picker;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;

import com.mission.schedule.R;
import com.mission.schedule.cutimage.Log;
import com.mission.schedule.my160920.kankan.wheel.widget.DateObject;
import com.mission.schedule.my160920.kankan.wheel.widget.OnWheelChangedListener;
import com.mission.schedule.my160920.kankan.wheel.widget.StringWheelAdapter;
import com.mission.schedule.my160920.kankan.wheel.widget.WheelView;

/**
 * 自定义的日期选择器
 *
 * @author sxzhang
 */
public class DatePicker extends LinearLayout {

    private Calendar calendar = Calendar.getInstance(); //������
    private WheelView newDays;
    private ArrayList<DateObject> dateList;
    private OnChangeListener onChangeListener; //onChangeListener
    private final int MARGIN_RIGHT = 0;
    private DateObject dateObject;        //日期数据对象

    //Constructors
    public DatePicker(Context context) {
        super(context);
        init(context);
    }

    public DatePicker(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 初始化
     *
     * @param context
     */
    private void init(Context context) {
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int currentitem = 0;

        dateList = new ArrayList<DateObject>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        int years= calendar.get(Calendar.YEAR);
        int m=1;//月份计数
        while (m<13)
        {
            int months=m;
            Calendar cal=Calendar.getInstance();//获得当前日期对象
            cal.clear();//清除信息
            cal.set(Calendar.YEAR,years);
            cal.set(Calendar.MONTH,months - 1);//1月从0开始
            cal.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天


            int count=cal.getActualMaximum(Calendar.DAY_OF_MONTH);


            for (int j=0;j<count;j++)
            {
                dateObject = new DateObject(years,
                        cal.get(Calendar.MONTH) + 1,
                        cal.get(Calendar.DAY_OF_MONTH) + j ,
                        cal.get(Calendar.DAY_OF_WEEK) + j,cal);
                dateList.add(dateObject);
                if (month == (cal.get(Calendar.MONTH) + 1) && day == (cal.get(Calendar.DAY_OF_MONTH) + j)){
                    currentitem = dateList.size() - 1;
                }
            }
            m++;
        }

        newDays = new WheelView(context);

        LayoutParams newDays_param = new LayoutParams(600, LayoutParams.WRAP_CONTENT);
        newDays_param.setMargins(0, 0, MARGIN_RIGHT, 0);
        newDays.setLayoutParams(newDays_param);
        newDays.setAdapter(new StringWheelAdapter(dateList, 7));
        newDays.setVisibleItems(5);
        newDays.setCyclic(false);

        newDays.setCurrentItem(currentitem);
        newDays.addChangingListener(onDaysChangedListener);
        addView(newDays);
    }

    /**
     * 滑动改变监听器
     */
    private OnWheelChangedListener onDaysChangedListener = new OnWheelChangedListener() {
        @Override
        public void onChanged(WheelView mins, int oldValue, int newValue) {
            calendar.set(Calendar.DAY_OF_MONTH, newValue + 1);
            change();
        }
    };

    /**
     * 滑动改变监听器回调的接口
     */
    public interface OnChangeListener {
        void onChange(int year, int month, int day, int day_of_week);
    }

    /**
     * 设置滑动改变监听器
     *
     * @param onChangeListener
     */
    public void setOnChangeListener(OnChangeListener onChangeListener) {
        this.onChangeListener = onChangeListener;
    }

    /**
     * 滑动最终调用的方法
     */
    private void change() {
        if (onChangeListener != null) {
            onChangeListener.onChange(
                    dateList.get(newDays.getCurrentItem()).getYear(),
                    dateList.get(newDays.getCurrentItem()).getMonth(),
                    dateList.get(newDays.getCurrentItem()).getDay(),
                    dateList.get(newDays.getCurrentItem()).getWeek());
        }
    }


    /**
     * 根据day_of_week得到汉字星期
     *
     * @return
     */
    public static String getDayOfWeekCN(int day_of_week) {
        String result = null;
        switch (day_of_week) {
            case 1:
                result = "星期日";
                break;
            case 2:
                result = "星期一";
                break;
            case 3:
                result = "星期二";
                break;
            case 4:
                result = "星期三";
                break;
            case 5:
                result = "星期四";
                break;
            case 6:
                result = "星期五";
                break;
            case 7:
                result = "星期六";
                break;
            default:
                break;
        }
        return result;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


}
